var t = getApp().globalData.api, e = getApp();

Page({
    data: {
        isSearch: !1,
        isSearchEnd: !1,
        searchValue: "",
        searchCityList: [],
        cityLocationHeight: 60,
        locationStart: !1,
        pyArray: [],
        allCityArray: [],
        hotCityArray: [],
        scrollTopId: "",
        currentScrollId: "",
        heightArr: [],
        cp_index: 0
    },
    onLoad: function(t) {
        var a = this, i = 50;
        wx.createSelectorQuery().in(this).select(".search-nav").boundingClientRect(function(t) {
            t.height > 0 && (i = t.height, a.setData({
                searchBarheight: i
            }));
        }).exec(), this.setData({
            searchBarheight: i,
            listHeight: wx.getSystemInfoSync().windowHeight,
            localCity: wx.getStorageSync("localCity"),
            localCityAreaCode: wx.getStorageSync("localCityAreaCode"),
            selectedCity: e.globalData.selectedCity,
            selectedCityAreaCode: e.globalData.selectedCityAreaCode
        }), this.getRequestCityData(), this.locateTap();
    },
    getRequestCityData: function() {
        var e = this;
        var openid = wx.getStorageSync('openid'); 
        t.post1("wechat/home/cityList", {
            openid: 'xjsdfmms'
        }).then(function(t) { 
            if ("1" == t.code) {
                console.log(t);
                for (var a = t.data.list, i = [], o = [], c = [], r = 0; r < a.length; r++) {
                    var n = a[r];
                    "热门" == n.groupname ? (n.groupname = "热门", o = n.CityDTOList) : i.push(n), 
                    c.push(n.groupname);
                }
                e.setData({
                    values: a,
                    hotCityArray: o,
                    allCityArray: i,
                    pyArray: c
                });
            }
        });
    },
    locateTap: function(t) {
        var a = this;
        this.setData({
            locationStart: !0
        }), e.firstLocation().then(function(t) {
            null != t ? (console.log(t), a.getCityCode(t.province, t.city, t.latitude, t.longitude)) : a.setData({
                locationStart: !1
            });
        }).then(function() {
            a.setData({
                locationStart: !1
            });
        });
    },
    getCityCode: function(a, i, o, c) {
        var r = this, n = {
            province: a,
            city: i
        };
        t.post1("wechat/home/getlocalcityinfo", n, !1).then(function(t) {
            if ("1" == t.code) {
              
                var a = t.data.areacode;
                e.globalData.localCity = i, e.globalData.localCityAreaCode = a, e.globalData.localLatitude = o, 
                e.globalData.localLongitude = c, r.setData({
                    locationStart: !1,
                    localCity: i,
                    localCityAreaCode: a,
                    selectedCity: i,
                    selectedCityAreaCode: a
                });
            }
        }, function(t) {
            r.setData({
                locationStart: !1
            });
        });
    },
    letterTap: function(t) {
        this.setData({
            scrollTopId: t.currentTarget.dataset.item,
            cp_index: t.currentTarget.dataset.index
        });
    },
    localCityTap: function(t) {
        this.back(this.data.selectedCity, this.data.selectedCityAreaCode);
    },
    selectCity: function(t) {
        console.log(t.target.dataset.cityname, t.target.dataset.areacode), this.back(t.target.dataset.cityname, t.target.dataset.areacode);
    },
    back: function(t, a) {
        e.globalData.selectedCity = t, e.globalData.selectedCityAreaCode = a, wx.setStorageSync("selectedCity", t), 
        wx.setStorageSync("selectedCityAreaCode", a), wx.navigateBack({
            delta: 1
        });
    },
    cityScroll: function(t) {
        if (0 == this.data.heightArr.length) {
            var e = this, a = 0, i = [];
            wx.createSelectorQuery().selectAll(".city-list-item").boundingClientRect(function(t) {}).exec(function(t) {
                t[0].forEach(function(t) {
                    a += t.height, i.push(a);
                }), e.setData({
                    heightArr: i
                });
            });
        }
        for (var o = this.data.classSelectIndex, c = this.data.oneShow, r = t.detail.scrollTop, n = this.data.heightArr, s = 0; s < n.length; s++) r >= 0 && r < n[0] ? c && this.setData({
            cp_index: 0,
            oneShow: !1
        }) : r >= n[s - 1] && r < n[s] && s != o && this.setData({
            cp_index: s,
            oneShow: !0
        });
    },
    searchValueInput: function(t) {
        var e = this.trim(t.detail.value);
        this.setData({
            searchValue: e,
            isSearch: e.length > 0,
            isSearchEnd: !1
        }), e.length > 0 && this.cityMatching(this.data.values, e);
    },
    searchCleanTap: function() {
        this.setData({
            searchValue: "",
            isSearch: !1,
            isSearchEnd: !1,
            searchCityList: []
        });
    },
    cityMatching: function(t, e) {
        var a = [];
        t.forEach(function(t, i) {
            "hot" != t.groupname && t.CityDTOList.forEach(function(t, i) {
                (t.cityname.indexOf(e) >= 0 || t.pinyin && t.pinyin.indexOf(e.toLowerCase()) >= 0) && a.push(t);
            });
        }), this.setData({
            isSearchEnd: !0,
            searchCityList: a
        });
    },
    trim: function(t) {
        return t.replace(/(^\s*)|(\s*$)/g, "");
    },
    onShareAppMessage: function() {}
});